/*
 * @Author: yayalee yangcheng960813@163.com
 * @Date: 2023-10-31 22:36:46
 * @LastEditTime: 2023-11-03 22:56:16
 * @LastEditors: yayalee yangcheng960813@163.com
 * @Description: 
 * @FilePath: \double-11-war\assets\script\item\Message.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */
import { _decorator, Component, Input, macro, Node, SpriteFrame, tween, view } from 'cc';
import { ComponentBase } from '../../cocos-libs/_cmpt/ui/base/ComponentBase';
import _layer from '../../cocos-libs/_cmpt/_layer';
import { AbName, DialogName } from '../default/Default';
import _cc from '../../cocos-libs/_cmpt/_cc';
import _js from '../../cocos-libs/_/_js';
import _res from '../../cocos-libs/_cmpt/_res';
import CustomSprite from '../../cocos-libs/_cmpt/ui/res/CustomSprite';
import { user } from '../data/UserInfo';
const { ccclass, property } = _decorator;

@ccclass('Message')
export class Message extends ComponentBase {

    @property(Node)
    private sp_icon!: Node;
    @property(Node)
    public rich_title!: Node;
    @property(Node)
    private rich_time!: Node;
    @property(Node)
    private sp_big_icon!: Node;
    @property(Node)
    private msg_0!: Node;
    @property(Node)
    private msg_1!: Node;
    @property(Node)
    private btn_close!: Node;

    private _time: number = 0;
    private _timer: number = 0;

    private _data!: IMessage;

    private _seed!: string;

    protected start(): void {
        super.start();
        // _cc.click(this.node, this.onMessageClicked, this);
        this.btn_close.click(this.close, this);
    }

    async init($data: IMessage, $seed: string, $time?: number) {
        this._data = $data;
        this._seed = $seed;
        const { timer, icon, msg_0, msg_1, title, big_icon } = $data;
        this._time = $time || Date.now();
        this._timer = this._time + (Number(timer) || 0);
        this.rich_title.setRichText(title);
        this.msg_0.setRichText(msg_0);
        this.msg_1.setRichText(msg_1);
        this.sp_icon.$(CustomSprite)?.setSpriteFrame(`${AbName.icon}${icon}`);
        big_icon ? this.sp_big_icon.$(CustomSprite)?.setSpriteFrame(`${AbName.icon}${big_icon}`) : this.sp_big_icon.hide();
        this.updateTime();
        this.schedule(this.updateTime, 1, macro.REPEAT_FOREVER);
    }

    updateTime() {
        ++this._time;
        let d = _js.formatterTimeData((this._timer - this._time) * 1000).toString();;
        this.rich_time.setRichText(`<color=red>${d}</color>`);
        if (this._time >= this._timer) {
            // this.onMessageClicked();
            console.log("消息自動激活");
            this.close();
        }
    }

    async onMessageClicked() {
        _layer.inst.openDialogAsync({ path: DialogName.redPacketDialog });
    }

    close() {
        const { width } = view.getVisibleSize();
        let x = this.node.x - width;
        tween(this.node).to(0.3, { x }).call(() => {
            //TODO : 后续换成对象池
            this.node.destroy();
            user.removeMsg(this._seed);
        }).start()
    }

}


